/**
 * Created by koudaiwang on 2017/6/21.
 */

var NewsTypeModel = require('../Dao/newstypedao');
var JsonModel = require('../config/jsonModel');


module.exports = function(app) {
    app.post('/addNewsType', function(req, res) {
        var model = new JsonModel.JsonModel();
        var name = req.body.name;
        var type = 1;
        model.status = 1;
        try {
            if (!name) {
                throw new Error('类型名称为空!');
            }
            if (type === -1) {
                throw new Error('类型为空');
            }
        } catch (e) {
            model.content = e.message;
            model.msg = e.message;
            return res.send(model);
        }
        var typeModel = {
            name: name,
            type: type
        };
        console.log(typeModel);
        NewsTypeModel.create(typeModel)
            .then(function (result) {
                console.log(result.ops[0]);
                var model = new JsonModel.JsonModel();
                model.msg = '成功';
                model.content = '插入成功!';
                res.send(model);
            }).catch(function(err){

            model.msg = JSON.stringify(err);
            model.content = "操作失败";
            return res.send(model);
        });
    });

    app.get('/getAllType', function (req, res) {
        var model = new JsonModel.JsonModel();
        model.status = 1;
        NewsTypeModel.getAllType().then(function (result) {
            model.msg = '成功';
            model.content = {types: result};
            res.send(model);
        }).catch(function (e) {
            model.status = 0;
            model.msg = e.message;
            model.content = e.message;
            res.send(model);
        })
    });
}